﻿// --------------------------------------------------------------------------------------------------------------------
// <copyright file="Cluster.cs" company="DissingNelleman">
//   
// All rights reserved. </copyright>
// <summary>
//   The cluster.
// </summary>
// --------------------------------------------------------------------------------------------------------------------

namespace ServiceLibrary.Services.Dto
{
    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Runtime.Serialization;

    /// <summary>
    /// The cluster.
    /// </summary>
    [DataContract]
    public class Cluster
    {
        #region Public Properties

        /// <summary>
        /// Gets or sets the date.
        /// </summary>
        [DataMember]
        public DateTime? Date { get; set; }

        /// <summary>
        /// Gets or sets the id.
        /// </summary>
        [DataMember]
        public Guid ID { get; set; }

        /// <summary>
        /// Gets or sets the location.
        /// </summary>
        [DataMember]
        public virtual Guid Location { get; set; }

        /// <summary>
        /// Gets or sets the nationality status.
        /// </summary>
        [DataMember]
        public virtual bool IsNational { get; set; }

        /// <summary>
        /// Gets or sets the methods.
        /// </summary>
        [DataMember]
        public virtual List<Guid> Methods { get; set; }

        /// <summary>
        /// Gets or sets the name.
        /// </summary>
        [DataMember]
        public virtual string Name { get; set; }

        /// <summary>
        /// Gets or sets the owner projects.
        /// </summary>
        [DataMember]
        public List<Guid> OwnerProjects { get; set; }

        /// <summary>
        /// Gets or sets the profiles.
        /// </summary>
        [DataMember]
        public virtual List<Guid> Profiles { get; set; }

        /// <summary>
        /// Gets or sets the statuses.
        /// </summary>
        [DataMember]
        public virtual List<Guid> Statuses { get; set; }

        #endregion

        #region Public Methods and Operators

        /// <summary>
        /// The convert.
        /// </summary>
        /// <param name="cluster">
        /// The cluster.
        /// </param>
        /// <returns>
        /// The <see cref="Cluster"/>.
        /// </returns>
        public static Cluster Convert(Models.Domain.Cluster cluster)
        {
            if (cluster != null)
            {
                var dto = new Cluster
                              {
                                  ID = cluster.ID, 
                                  Name = cluster.Name, 
                                  Location = (cluster.Location != null ? cluster.Location.ID : Guid.Empty), 
                                  Date = cluster.Date, 
                                  Methods = (from m in cluster.Methods select m.ID).ToList(), 
                                  Profiles = (from p in cluster.Profiles select p.ID).ToList(), 
                                  Statuses = (from s in cluster.Statuses select s.ID).ToList()
                              };
                if (cluster.OwnerProjects != null && cluster.OwnerProjects.Any())
                {
                    dto.OwnerProjects = new List<Guid>();
                    foreach (Models.Domain.Project proj in cluster.OwnerProjects)
                    {
                        dto.OwnerProjects.Add(proj.ID);
                    }
                }

                return dto;
            }
            
            return null;
        }

        #endregion
    }
}

namespace ServiceLibrary.Services.Queries
{
    using System.Runtime.Serialization;

    using NHibernate;

    using ServiceLibrary.Actioners.Activities;
    using ServiceLibrary.Services.Results;
    using ServiceLibrary.Services.Dto;
    using System;
    using System.Collections.Generic;

    /// <summary>
    /// The cluster query.
    /// </summary>
    [DataContract]
    public class ClusterQuery : IQuery
    {
        #region Public Methods and Operators

        /// <summary>
        /// Gets or sets the default fields.
        /// </summary>
        [DataMember]
        public DefaultSearchFields DefaultFields { get; set; }

        /// <summary>
        /// Gets or sets the guid of the location.
        /// </summary>
        [DataMember]
        public Guid? AtLocation { get; set; }

        /// <summary>
        /// Gets or sets the method types.
        /// </summary>
        [DataMember]
        public List<Guid> MethodTypes { get; set; }
        
        /// <summary>
        /// Gets or sets the status types.
        /// </summary>
        [DataMember]
        public List<Guid> StatusTypes { get; set; }

        /// <summary>
        /// Gets or sets the list of profile ids that should be a part of the result.
        /// </summary>
        [DataMember]
        public List<Guid> Profiles { get; set; }

        /// <summary>
        /// Gets or sets whether the profiles are at remote a area.
        /// </summary>
        [DataMember]
        public bool? ProfilesAreAtRemoteArea { get; set; }

        /// <summary>
        /// Gets or sets if it's an entrepreneur.
        /// </summary>
        [DataMember]
        public bool? ProfilesAreEntrepreneur { get; set; }
        
        /// <summary>
        /// The execute.
        /// </summary>
        /// <param name="hsession">
        /// The hsession.
        /// </param>
        /// <returns>
        /// The <see cref="IResult"/>.
        /// </returns>
        public override IResult Execute(ISession hsession)
        {
            var actioner = new ClusterActioner(hsession, null);

            return actioner.ExecuteSearchQuery(this);
        }

        #endregion
    }
}

namespace ServiceLibrary.Services.Results
{
    using System.Collections.Generic;
    using System.Runtime.Serialization;

    using ServiceLibrary.Services.Dto;

    /// <summary>
    /// The cluster result.
    /// </summary>
    [DataContract]
    public class ClusterResult : IResult
    {
        #region Constructors and Destructors

        /// <summary>
        /// Initializes a new instance of the <see cref="ClusterResult"/> class.
        /// </summary>
        public ClusterResult()
        {
            this.List = new List<Cluster>();
        }

        #endregion

        #region Public Properties

        /// <summary>
        /// Gets or sets the default fields.
        /// </summary>
        [DataMember]
        public DefaultSearchFields DefaultFields { get; set; }

        /// <summary>
        /// Gets or sets the list.
        /// </summary>
        [DataMember]
        public List<Cluster> List { get; set; }

        /// <summary>
        /// Gets or sets the total row count.
        /// </summary>
        [DataMember]
        public int TotalRowCount { get; set; }

        #endregion
    }
}

namespace ServiceLibrary.Services.Commands
{
    using System;
    using System.Runtime.Serialization;

    using NHibernate;

    using ServiceLibrary.Actioners.Activities;
    using ServiceLibrary.Services.Dto;

    using User = ServiceLibrary.Models.Domain.User;

    /// <summary>
    /// The create cluster command.
    /// </summary>
    [DataContract]
    public class CreateClusterCommand : ICommand
    {
        #region Public Properties

        /// <summary>
        /// Gets or sets the cluster.
        /// </summary>
        [DataMember]
        public Cluster Cluster { get; set; }

        #endregion

        #region Public Methods and Operators

        /// <summary>
        /// The execute.
        /// </summary>
        /// <param name="hsession">
        /// The hsession.
        /// </param>
        /// <param name="activeUser">
        /// The active user.
        /// </param>
        public override void Execute(ISession hsession, User activeUser)
        {
            var actioner = new ClusterActioner(hsession, activeUser);
            actioner.CreateCluster(this);
        }

        #endregion
    }

    /// <summary>
    /// The update cluster command.
    /// </summary>
    [DataContract]
    public class UpdateClusterCommand : ICommand
    {
        #region Public Properties

        /// <summary>
        /// Gets or sets the dto.
        /// </summary>
        [DataMember]
        public Cluster Dto { get; set; }

        #endregion

        #region Public Methods and Operators

        /// <summary>
        /// The execute.
        /// </summary>
        /// <param name="hsession">
        /// The hsession.
        /// </param>
        /// <param name="activeUser">
        /// The active user.
        /// </param>
        public override void Execute(ISession hsession, User activeUser)
        {
            var actioner = new ClusterActioner(hsession, activeUser);
            actioner.UpdateCluster(this);
        }

        #endregion
    }

    /// <summary>
    /// The delete cluster command.
    /// </summary>
    [DataContract]
    public class DeleteClusterCommand : ICommand
    {
        #region Public Properties

        /// <summary>
        /// Gets or sets the dto.
        /// </summary>
        [DataMember]
        public Guid? Dto { get; set; }

        #endregion

        #region Public Methods and Operators

        /// <summary>
        /// The execute.
        /// </summary>
        /// <param name="hsession">
        /// The hsession.
        /// </param>
        /// <param name="activeUser">
        /// The active user.
        /// </param>
        public override void Execute(ISession hsession, User activeUser)
        {
            var actioner = new ClusterActioner(hsession, activeUser);
            actioner.DeleteCluster(this);
        }

        #endregion
    }
}